<template>
  <div>
    <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
      <div class="flex">
        <div
          class="iconbox flexcenter commhover blackbg mr_10"
          @click="resetQuery"
        >
          <i class="el-icon-refresh f-20" :class="{ rotate: isrefresh }"></i>
        </div>
        <el-button plain type="primary" size="mini" @click="handleAdd">添加</el-button>
        <el-button plain type="success" size="mini" @click="handleLog">管理渠道</el-button>
      </div>
      <div>
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="0"
        >
          <el-form-item prop="name">
            <el-input
              class="searchinput"
              v-model="queryParams.name"
              placeholder="请输入用户名/昵称/手机号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="mr_0">
            <el-dropdown trigger="click">
              <el-button size="mini" class="nor_border plr_9">
                <svg-icon class-name="f-14" icon-class="listicon" />
                <i class="el-icon-caret-bottom el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <div class="dropbody">
                  <el-checkbox-group
                    v-model="checkedOption"
                    class="flexcolumn"
                    @change="changeSelect"
                  >
                    <el-checkbox
                      v-for="(item, index) in selectoptions"
                      :label="item.label"
                      :key="index"
                      class="m_5"
                      >{{ item.label }}</el-checkbox
                    >
                  </el-checkbox-group>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item>
            <el-dropdown trigger="click" @command="handleCommand">
              <el-button size="mini" class="nol_border plr_9">
                <svg-icon class-name="f-14" icon-class="export" />
                <i class="el-icon-caret-bottom el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  v-for="(item, index) in exportlist"
                  :key="index"
                  :command="item.id"
                  >{{ item.name }}</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item>
            <el-button
              class="f-14"
              type=""
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            ></el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="list"
      border
      ref="tablelayout"
      class="commtable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        label="ID"
        prop="id"
        v-if="memberOption.includes('ID')"
      ></el-table-column>
      <el-table-column
        label="用户名"
        prop="platName"
        width="120"
        align="center"
        v-if="memberOption.includes('用户名')"
      >
        <template slot-scope="scope">
          {{ scope.row.platName }} 智选评选
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('会员等级')"
        label="会员等级"
        width="120"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.activeid }} 抽奖</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('手机号')"
        label="手机号"
        prop="name"
        width="200"
        align="center"
      />
      <el-table-column
        v-if="memberOption.includes('余额')"
        label="余额"
        prop="name"
        width="100"
        align="center"
      />
      <el-table-column
        v-if="memberOption.includes('开通时间')"
        label="开通时间"
        prop="time"
        width="200"
        align="center"
      />
      <el-table-column
        v-if="memberOption.includes('有效期')"
        label="有效期"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}1/50</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('登录时间')"
        label="登录时间"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}2023-06-19 14:36:00</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('注册时间')"
        label="注册时间"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}2023-06-19 14:36:00</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('渠道')"
        label="渠道"
        align="center"
      >
        <template slot-scope="scope">
          <span>小程序{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('状态')"
        label="状态"
        align="center"
        width="160"
      >
        <template slot-scope="scope">
          <el-tag type="success" class="tag_success">{{ scope.row.status }}正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        v-if="memberOption.includes('活动数量')"
        label="活动数量"
        align="center"
      >
        <template slot-scope="scope">
          <span>3{{ scope.row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="操作"
        width="350"
        fixed="right"
        align="center"
      >
        <template slot-scope="scope">
          <div class="flex btnflex">
            <div
              class="commhover btn_green mr_10 c_fff"
              @click="handleFabu(scope.row)"
            >
              <svg-icon icon-class="fabu" class-name="f-14" />前台
            </div>
            <div
              class="commhover btn_yellow mr_10 c_fff"
              @click="handleMember(scope.row)"
            >
              <i class="el-icon-edit-outline f-14"></i>开通会员
            </div>
            <div
              class="commhover btn_blue mr_10 c_fff"
              @click="handleCz(scope.row)"
            >
              <svg-icon icon-class="yulan" class-name="f-14" />充值
            </div>
            <div
              class="commhover btn_green mr_10 c_fff"
              @click="handleEdit(scope.row)"
            >
              <i class="el-icon-delete f-14"></i> 编辑
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.num"
      @pagination="getList"
    />

    <el-dialog
      :title="isadd ? '添加' : '编辑'"
      :visible.sync="dialogAddVisible"
      width="680px"
      class="dialogheaderbg"
      :before-close="handleCloseAdd"
    >
      <add-member
        ref="addmember"
        :isadd="isadd"
        :row="row"
        @close="handleCloseAdd"
      />
    </el-dialog>
    <!-- 开通会员 -->
    <el-dialog
      title="开通会员"
      :visible.sync="dialogVisible"
      width="560px"
      class="dialogheaderbg"
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form"
        label-width="100px"
        label-position="left"
      >
        <el-form-item v-if="isadd" label="会员等级:" prop="level">
          <el-select
            v-model="form.level"
            style="width: 100%"
            placeholder="请选择会员等级"
          >
            <el-option
              v-for="(item, index) in levelList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="isadd" label="有效期:" prop="time">
          <el-select
            v-model="form.time"
            placeholder="请选择日期"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in timeList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="充值"
      :visible.sync="dialogCzVisible"
      width="680px"
      class="dialogheaderbg"
      :before-close="handleCloseCz"
    >
      <cz-member :row="row" @close="handleCloseCz" />
    </el-dialog>

    <!-- 渠道管理 -->
    <el-dialog
      class="dialogheaderbg"
      title="渠道管理"
      :visible.sync="dialogQdVisible"
      width="80%"
      :before-close="handleQdClose"
    >
      <qd-table @close="handleQdClose" />
    </el-dialog>

    <!-- 预览 -->
    <!-- <el-dialog
      title=""
      :visible.sync="dialogView"
      width="1000px"
      :show-close="false"
      class="noheaderdia"
      :before-close="handleViewClose"
    >
      <view-comp :rowId="rowId" @close="handleViewClose" />
    </el-dialog> -->
  </div>
</template>
<script>
import { mapState } from "vuex";
import AddMember from "./components/AddMember.vue";
import CzMember from './components/CzMember.vue'
import QdTable from './components/QdTable.vue'

export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        name: undefined,
      },
      total: 1,
      loading: false,
      list: [{ id: 1 }, { id: 2 }],
      checkedOption: [],
      isrefresh: false,
      selectoptions: [
        { id: 1, label: "ID" },
        { id: 2, label: "用户名" },
        { id: 3, label: "会员等级" },
        { id: 4, label: "手机号" },
        { id: 5, label: "余额" },
        { id: 6, label: "开通时间" },
        { id: 7, label: "有效期" },
        { id: 8, label: "登录时间" },
        { id: 9, label: "注册时间" },
        { id: 10, label: "渠道" },
        { id: 11, label: "状态" },
        { id: 12, label: "活动数量" },
      ],
      dialogAddVisible: false,
      multiple: true,
      isadd: true,
      title: "",
      row: null,
      levelList: [
        { label: "免费版", value: 1 },
        { label: "专业版", value: 2 },
        { label: "旗舰版", value: 3 },
      ],
      timeList: [
        { label: "1个月", value: 1 },
        { label: "一个季度", value: 2 },
        { label: "一年", value: 3 },
      ],
      dialogVisible:false,
      form: {},
      rules:{
        level: [
          { required: true, message: "请选择会员等级", trigger: "change" },
        ],
        time: [{ required: true, message: "请选择有效期", trigger: "change" }]
      },
      dialogCzVisible:false,

      dialogQdVisible:false,
      // 日志
      // dialoglogVisible: false,
      // rowId: "",
      // 预览
      // dialogView: false,
    };
  },

  props: {
    type: {
      type: Number,
      default: 1,
    },
  },

  components: {
    AddMember,
    CzMember,
    QdTable
    // FabuComp,
    // LogTable,
    // ViewComp,
  },

  computed: {
    ...mapState({
      memberOption: (state) => state.paramsoption.memberOption,
    }),
  },

  created() {
    let memberOption = JSON.parse(localStorage.getItem("memberOption"));
    if (memberOption) {
      this.changeSelect(memberOption);
    }
    this.checkedOption = this.memberOption;
  },

  methods: {
    getList() {
      this.loading = true;
      let params = { ...this.queryParams };
      //   if(this.queryParams.type=='创建日期' && this.time && this.time.length){
      //     params.startime = this.time[0]
      //     params.endtime = this.time[1]
      //   }
      //   feedbacklist(params).then((response) => {
      //     this.tableData = response.data.list;
      //     this.total = response.data.count;
      //     this.loading = false;
      //   });
      this.isrefresh = false;
      this.loading = false;
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.isrefresh = true;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.id);
      // this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    changeSelect(arr) {
      this.$store.commit("SET_MEMBEROPTION", arr);
      this.$nextTick(() => {
        //在数据加载完，重新渲染表格
        this.$refs.tablelayout && this.$refs.tablelayout.doLayout();
      });
    },

    handleCommand(command) {
      console.log(command);
    },

    handleAdd() {
      this.isadd = true;
      this.dialogAddVisible = true;
    },

    handleCloseAdd() {
      this.$refs.addmember.resetForm();
      this.dialogAddVisible = false;
    },

    handleEdit(row) {
      this.row = row;
      this.isadd = false;
      this.dialogAddVisible = true;
    },

    handleMember(row){
      // this.row = row
      this.dialogVisible = true
    },

    submit(){
      this.$refs.form.validate(valid=>{
        if(!valid) return

      })
    },

    handleClose() {
      this.dialogVisible = false;
    },

    handleCz(row) {
      this.dialogCzVisible = true;
    },

    handleCloseCz(type){
      if(type == 1){
        this.getList()
      }
      this.dialogCzVisible = false
    },

    handleLog() {
      this.dialogQdVisible = true;
    },

    handleQdClose() {
      this.dialogQdVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 26px;
}
.imgmini {
  width: 40px;
  height: 40px;
  margin: auto;
}
.imgbox {
  width: 120px;
  height: 120px;
}
</style>
<style lang="scss">
.noheaderdia {
  .el-dialog__header {
    padding: 0 !important;
  }
  .el-dialog__body {
    padding: 0 !important;
  }
}
</style>